﻿/*
	* Project	: SharePoint Dynamic Forms
	* Version	: 1.0
	* Company	: SharePoint Frontier Software Pvt Ltd. (http://www.sharepointfrontier.com)
	* Author	: Ashok Raja .T (Blog : http://www.ashokraja.me)
	* Licence	: Published under Microsoft Public License (Ms-PL)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SFS.SharePoint.DynamicForms.Validation
{
    public class NumericValidator
    {
        public static bool GreaterThan(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            return Validate(rule, Field, "g");
        }

        public static bool GreaterThanEqual(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            return Validate(rule, Field, "ge");
        }

        public static bool LessThan(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            return Validate(rule, Field, "l");
        }

        public static bool LessThanEqual(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            return Validate(rule, Field, "le");
        }

        public static bool Equal(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            return Validate(rule, Field, "e");
        }

        public static bool NotEqual(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            return Validate(rule, Field, "ne");
        }

        private static bool Validate(SPDynamicFieldValidationRule rule, SPDynamicField Field, string Operator)
        {
            try
            {
                decimal FieldValue = 0;
                decimal RuleValue = 0;
                bool isPercentage = false;

                if (rule.ValidateAgainst.EndsWith("%"))
                    isPercentage = true;

                if (!decimal.TryParse(Field.Value.ToString(), out FieldValue))
                {
                    rule.ValidationMessage = Field.DisplayName + " is not a valid number.";
                    rule.IsValid = false;
                    return false;
                }

                if (!decimal.TryParse(rule.ValidateAgainst.TrimEnd('%'), out RuleValue))
                {
                    rule.ValidationMessage = rule.ValidationType + " validation rule is invalid for " + Field.DisplayName + ".";
                    rule.IsInvalidRule = true;
                    rule.IsValid = false;
                    return false;
                }

                if (isPercentage)
                    FieldValue = FieldValue * 100;

                switch (Operator)
                {
                    case "g":
                        if (FieldValue > RuleValue) { return true; }
                        else
                        {
                            rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, Field.DisplayName + " must be greater than " + RuleValue.ToString() + ".");
                            rule.IsValid = false;
                            return false;
                        }
                    case "ge":
                        if (FieldValue >= RuleValue) { return true; }
                        else
                        {
                            rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, Field.DisplayName + " must be greater than or equal " + RuleValue.ToString() + ".");
                            rule.IsValid = false;
                            return false;
                        }

                    case "l":
                        if (FieldValue < RuleValue) { return true; }
                        else
                        {
                            rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, Field.DisplayName + " must be less than " + RuleValue.ToString() + ".");
                            rule.IsValid = false;
                            return false;
                        }
                    case "le":
                        if (FieldValue <= RuleValue) { return true; }
                        else
                        {
                            rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, Field.DisplayName + " must be less than " + RuleValue.ToString() + ".");
                            rule.IsValid = false;
                            return false;
                        }
                    case "e":
                        if (FieldValue == RuleValue) { return true; }
                        {
                            rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, Field.DisplayName + " must be equal to " + RuleValue.ToString() + ".");
                            rule.IsValid = false;
                            return false;
                        }
                    case "ne":
                        if (FieldValue != RuleValue) { return true; }
                        {
                            rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, Field.DisplayName + " must be equal to " + RuleValue.ToString() + ".");
                            rule.IsValid = false;
                            return false;
                        }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
